//minimize
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 1e6 + 10;
int n, C, a[N], b[N], tot = 1;

struct node{
	ll ad, mx, sp;
	int ls, rs, l, r, tg;
} t[N*4];

void psd(int p){
	int mid = t[p].l + t[p].r >> 1;
	if(!t[p].ls && t[p].l != t[p].r){
		t[p].ls = ++ tot;
		t[t[p].ls].l = t[p].l;
		t[t[p].ls].r = mid;
	}
	if(!t[p].rs && t[p].l != t[p].r){
		t[p].rs = ++ tot;
		t[t[p].rs].l = mid + 1;
		t[t[p].rs].r = t[p].r;
	}
	if(t[p].sp != -1){
		t[t[p].ls].mx = t[t[p].rs].mx = t[p].sp;
		t[t[p].ls].tg = t[t[p].ls].ad = 0;
		t[t[p].ls].sp = t[p].sp;
		t[t[p].rs].tg = t[t[p].rs].ad = 0;
		t[t[p].rs].sp = t[p].sp;
		t[p].sp = -1;
	}
	t[t[p].ls].tg += t[p].tg;
	t[t[p].ls].mx += (ll)t[p].tg * b[t[t[p].ls].r];
	t[t[p].rs].tg += t[p].tg;
	t[t[p].rs].mx += (ll)t[p].tg * b[t[t[p].rs].r];
	t[p].tg = 0;
	t[t[p].ls].ad += t[p].ad;
	t[t[p].ls].mx += t[p].ad;
	t[t[p].rs].ad += t[p].ad;
	t[t[p].rs].mx += t[p].ad;
	t[p].ad = 0;
}
void psu(int p){
	t[p].mx = max(t[t[p].ls].mx, t[t[p].rs].mx);
}

void add(int p, int l, int r, int ql, int qr, ll v){
	if(qr < l || r < ql) return;
	if(ql <= l && r <= qr){
		t[p].mx += v;
		t[p].ad += v;
	} else {
		int mid = l + r >> 1;
		psd(p);
		add(t[p].ls, l, mid, ql, qr, v);
		add(t[p].rs, mid+1, r, ql, qr, v);
		psu(p);
	}
}
void tag(int p, int l, int r, int ql, int qr){
	if(qr < l || r < ql) return;
	if(ql <= l && r <= qr){
		t[p].mx += b[t[p].r];
		++ t[p].tg;
	} else {
		int mid = l + r >> 1;
		psd(p);
		tag(t[p].ls, l, mid, ql, qr);
		tag(t[p].rs, mid+1, r, ql, qr);
		psu(p);
	}
}
void mdf(int p, int l, int r, int ql, int qr, ll v){
	if(qr < l || r < ql) return;
	if(ql <= l && r <= qr){
		t[p].mx = t[p].sp = v;
		t[p].tg = t[p].ad = 0;
	} else {
		int mid = l + r >> 1;
		psd(p);
		mdf(t[p].ls, l, mid, ql, qr, v);
		mdf(t[p].rs, mid+1, r, ql, qr, v);
		psu(p);
	}
}
ll qry(int p, int l, int r, int x){
	if(l == r){
		return t[p].mx;
	} else {
		int mid = l + r >> 1;
		psd(p);
		if(x <= mid) return qry(t[p].ls, l, mid, x);
		else return qry(t[p].rs, mid+1, r, x);
	}
}
int fnd(int p, int l, int r, int ql, int qr, ll v){
	if(l == r){
		return l;
	} else {
		int mid = l + r >> 1;
		psd(p);
		if(t[t[p].ls].mx >= v){
			return fnd(t[p].ls, l, mid, ql, qr, v);
		} else {
			return fnd(t[p].rs, mid+1, r, ql, qr, v);
		}
	}
}

int main(){
	freopen("minimize.in", "r", stdin);
	freopen("minimize.out", "w", stdout);
	scanf("%d%d", &n, &C);
	for(int i = 1; i <= n; ++ i){
		scanf("%d", &a[i]);
		b[i] = a[i];
	}
	sort(b + 1, b + n + 1);
	int mx = unique(b + 1, b + n + 1) - b - 1;
	t[1].l = 1, t[1].r = mx;
	for(int i = 1; i <= n; ++ i){
		a[i] = lower_bound(b + 1, b + mx + 1, a[i]) - b;
		add(1, 1, mx, 1, mx, C);
		add(1, 1, mx, a[i], mx, -b[a[i]]-C);
		tag(1, 1, mx, a[i], mx);
		ll val = qry(1, 1, mx, a[i]);
		int p = fnd(1, 1, mx, 1, a[i]-1, val);
		mdf(1, 1, mx, p, a[i]-1, val);
	}
	printf("%lld\n", qry(1, 1, mx, 1));
	return 0;
}